package com.iseastar.guojiang.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.shouhuobao.station.R;
import com.shouhuobao.take.weight.datepicker.NumberPicker;

import java.util.Calendar;

/**
 * Created by joker on 2018/1/30.
 */
public class TimePicker extends LinearLayout implements NumberPicker.OnValueChangeListener {

    private NumberPicker mHourPicker;
    private NumberPicker mMinutePicker;

    private OnDateChangedListener mOnDateChangedListener;

    private LayoutInflater mLayoutInflater;

    private String currentHourStr = "00";
    private String currentMinuteStr = "00";

    String[] hourNames = new String[]{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
    String[] minuteNames = new String[]{"00", "10", "20", "30", "40", "50"};

    public TimePicker(Context context) {
        this(context, null);
    }

    public TimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        init();
    }

    private void init() {
        mLayoutInflater.inflate(R.layout.layout_courier_time_picker, this, true);
        mHourPicker = (NumberPicker) findViewById(R.id.hour_picker);
        mMinutePicker = (NumberPicker) findViewById(R.id.minute_picker);

        mHourPicker.setOnValueChangeListener(this);
        mMinutePicker.setOnValueChangeListener(this);


        mHourPicker.setCustomTextArray(hourNames);
        mHourPicker.setEndNumber(23);
        mMinutePicker.setCustomTextArray(minuteNames);
        mMinutePicker.setEndNumber(5);
    }

    @Override
    public void onValueChange(final NumberPicker picker, final int oldVal, final int newVal) {
        if (picker == mHourPicker) {
            for (int i = 0; i < hourNames.length; i++) {
                if (i == newVal) {
                    currentHourStr = hourNames[i];
                    break;
                }
            }
        } else if (picker == mMinutePicker) {
            for (int i = 0; i < minuteNames.length; i++) {
                if (i == newVal) {
                    currentMinuteStr = minuteNames[i];
                    break;
                }
            }
        }
        notifyDateChanged();
    }

    /**
     * The callback used to indicate the user changes\d the date.
     */
    public interface OnDateChangedListener {

        /**
         * Called upon a date change.
         *
         * @param view   The view associated with this listener.
         * @param hour   The year that was set.
         * @param minute The month that was set (0-11) for compatibility
         *               with {@link Calendar}.
         */
        void onDateChanged(TimePicker view, String hour, String minute);
    }

    public TimePicker setOnDateChangedListener(OnDateChangedListener l) {
        mOnDateChangedListener = l;
        return this;
    }

    private void notifyDateChanged() {
        if (mOnDateChangedListener != null) {
            mOnDateChangedListener.onDateChanged(this, currentHourStr, currentMinuteStr);
        }
    }

    @Override
    public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
        super.setSoundEffectsEnabled(soundEffectsEnabled);
        mHourPicker.setSoundEffectsEnabled(soundEffectsEnabled);
        mMinutePicker.setSoundEffectsEnabled(soundEffectsEnabled);
    }

    public TimePicker setRowNumber(int rowNumber) {
        mHourPicker.setRowNumber(rowNumber);
        mMinutePicker.setRowNumber(rowNumber);
        return this;
    }

    public TimePicker setTextSize(float textSize) {
        mHourPicker.setTextSize(textSize);
        mMinutePicker.setTextSize(textSize);
        return this;
    }

    public TimePicker setFlagTextSize(float textSize) {
        mHourPicker.setFlagTextSize(textSize);
        mMinutePicker.setFlagTextSize(textSize);
        return this;
    }

    public TimePicker setTextColor(int color) {
        mHourPicker.setTextColor(color);
        mMinutePicker.setTextColor(color);
        return this;
    }

    public TimePicker setFlagTextColor(int color) {
        mHourPicker.setFlagTextColor(color);
        mMinutePicker.setFlagTextColor(color);
        return this;
    }

    public TimePicker setBackground(int color) {
        super.setBackgroundColor(color);
        mHourPicker.setBackground(color);
        mMinutePicker.setBackground(color);
        return this;
    }

    public TimePicker setCurrentHour(String hour) {
        Integer index = null;
        for (int i = 0; i < hourNames.length; i++) {
            if (hourNames[i].equals(hour)) {
                index = i;
                currentHourStr = hour;
                break;
            } else {
                index = null;
            }
        }
        if (index == null) {
            mHourPicker.setCurrentNumber(0);
        } else {
            mHourPicker.setCurrentNumber(index);
        }
        return this;
    }

    public TimePicker setCurrentMinute(String minute) {
        Integer index = null;
        for (int i = 0; i < minuteNames.length; i++) {
            if (minuteNames[i].equals(minute)) {
                index = i;
                currentMinuteStr = minute;
                break;
            } else {
                index = null;
            }
        }
        if (index == null) {
            mMinutePicker.setCurrentNumber(0);
        } else {
            mMinutePicker.setCurrentNumber(index);
        }
        return this;
    }

}
